﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using com.tiyma.common;
using com.tiyma.components;
using com.tiyma.model;

namespace com.tiyma.web
{
    public partial class Book : BasePage
    {
        BookManager m_book_mgr = new BookManager();
        BookInfo m_book = null;

        string m_icon_template = " <img src=\"{0}\" alt=\"{1}\" />";

        string m_last_update_chapter = "<a href=\"/reader/{1}/{0}/\" target=\"_blank\">{2}</a>";

        string m_chapter_list_template = "<li><a href=\"/{3}/{1}/{0}/\" target=\"_blank\">{2}</a></li>";

        string m_recommend_book_template ="<li><span class=\"booktype\">[{0}]</span>&nbsp;&nbsp;<a title=\"最新章节：{1}\" target=\"_blank\" href=\"/book/{2}/\">{3}</a>&nbsp;&nbsp;<span class=\"authortype\"><a href=\"/bookList.aspx?k={4}&t=2\">{5}</a></span></li>";

        string m_top_template = "<a href=\"/bookList.aspx?k={0}&t=3\">{1}</a>&gt;&nbsp;{2}";

        public long m_book_id = 0;
        public string m_book_icon = string.Empty;
        public string m_status = "0";

        string m_book_name = string.Empty;
        const string m_no_description = "本书没收录到简介，却不能隐晦它内容的精彩，点击看看，或许是一本精彩佳作。";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!long.TryParse(Request["bookId"].ToString(), out m_book_id))
                m_book_id = 0;

            BindBook();
            BindRecommendBook();

            if (Token != null)
            {
                lblScript.Text = string.Format("<script>var loginUserId='{0}';var bookName='{1}';</script>", Token.UserId, m_book_name);
            }
        }

        /// <summary>
        /// 绑定推荐小说
        /// </summary>
        private void BindRecommendBook()
        {
            try
            {
                
                List<BookInfo> t_book_list = m_book_mgr.GetRecommendBook(Request, m_book.BookName, m_book.BookCategory, m_book.AuthorName);

                if (t_book_list != null && t_book_list.Count>0)
                {
                    
                    StringBuilder t_sb = new StringBuilder();
                    foreach(BookInfo t_book in t_book_list)
                    {


                        t_sb.AppendFormat(m_recommend_book_template, t_book.BookCategory,t_book.LastUpdateChapterName,t_book.BookId,t_book.BookName,HttpUtility.UrlEncode(t_book.AuthorName,Encoding.GetEncoding("gb2312")),t_book.AuthorName);
                    }
                   lblRecommend.Text = string.Format("<ul class=\"booklist1\">{0}</ul>", t_sb.ToString());
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定推荐小说失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 绑定小说信息
        /// </summary>
        private void BindBook()
        {
             List<ChapterInfo> t_list_chapter=null;
          
             m_book = m_book_mgr.GetBookInfo(m_book_id, out t_list_chapter);

             if (m_book == null)
             {
                 RedirectDefaultPage();
                 return;
             }

             lblAuthorName.Text = m_book.AuthorName;
             lblCategoryName.Text = m_book.BookCategory;
             lblChapterCount.Text = m_book.BookChapterCount.ToString();
             lblDescription.Text =string.IsNullOrEmpty( m_book.Description)?m_no_description:m_book.Description;
             lblUpdateTime.Text = m_book.LastUpdateTime.ToString("yyyy-MM-dd");
             m_book_icon = m_book.ICONUrl;
             divICON.InnerHtml = string.Format(m_icon_template,string.IsNullOrEmpty( m_book.ICONUrl)?"/images/noimg.jpg":m_book.ICONUrl, m_book.BookName);
             lblLastUpdateChapter.Text = string.Format(m_last_update_chapter,  m_book.LastUpdateChapterId,m_book.SiteId, SystemUtil.ChangeChapterName( m_book.LastUpdateChapterName));
             m_book_name = m_book.BookName;
             m_status = m_book.Status;
             StringBuilder sb = new StringBuilder();
             if (t_list_chapter != null)
             {
                 foreach (ChapterInfo t_chapter in t_list_chapter)
                 {
                     sb.AppendFormat(m_chapter_list_template, t_chapter.ChapterId, m_book.SiteId, t_chapter.ChapterName,  "reader" );
                 }

                 lblChapter.Text = string.Format("<ul>{0}</ul>", sb.ToString());
             }
             lblBookInfo.Text = string.Format(m_top_template, HttpUtility.UrlEncode(m_book.BookCategory), m_book.BookCategory, m_book.BookName);

                
           
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (this.Master is Common)
            {
                string metaDescription =
                    string.Format("{0}小说阅读,{4}提供{0}最新章节及章节列表,{1}小说{0}是作者{2}创作,{0}最新更新{0}小说章节,最新更新尽在{4}。{0}小说摘要:{3}",
                    m_book.BookName,m_book.BookCategory ,m_book.AuthorName ,
                    (m_book.Description != null && m_book.Description != "" ? (m_book.Description.Trim().Length >= 60 ? m_book.Description.Trim().Substring(0, 60).Replace("\"", "'").Replace("<br>", "").Replace("＜p＞", "").Replace("<p>", "").Replace("<", "＜").Replace(">", "＞").Replace("　　", " ") + "..." : m_book.Description.Trim().Replace("\"", "'")) : ""), "体验码小说网");


                Common master = (Common)this.Master;
                this.Title = string.Format("{0},及小说作者{1}的{0}最新章节尽体验码小说网", m_book.BookName, m_book.AuthorName);
                master.SetPageMetaKeyword(m_book.BookName + ",最新章节,全本,免费," + m_book.AuthorName + "," + m_book.BookCategory);
                master.SetPageMetaDescription(metaDescription);
                master.SetCssFile("/style/book.css");

            }
        }
    }
}
